//
//  AddTool.m
//  IceBoxStorage
//
//  Created by xieyanhong on 2023/8/24.
//

#import "AddTool.h"
#define kToolFile NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject
@implementation AddTool
+ (NSString *)saveImg:(UIImage *)image{
    NSString *imgFile = [NSString stringWithFormat:@"%d.png",(int)[[NSDate date]timeIntervalSince1970]*10000];
    BOOL isS = [UIImagePNGRepresentation(image) writeToFile:[NSString stringWithFormat:@"%@/%@",kToolFile,imgFile] atomically:YES];
    if(isS){
        return imgFile;
    }else{
        return @"";
    }
}
+ (UIImage *)getImgFile:(NSString *)file{
    UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",kDocumentFile,file]];
    return image;
}
+ (UIViewController *)currentVC{
    
    UIViewController* currentViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currentViewController.presentedViewController) {

            currentViewController = currentViewController.presentedViewController;
        } else if ([currentViewController isKindOfClass:[UINavigationController class]]) {

          UINavigationController* navigationController = (UINavigationController* )currentViewController;
            currentViewController = [navigationController.childViewControllers lastObject];

        } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {

          UITabBarController* tabBarController = (UITabBarController* )currentViewController;
            currentViewController = tabBarController.selectedViewController;
        } else {

            NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
            if (childViewControllerCount > 0) {

                currentViewController = currentViewController.childViewControllers.lastObject;

                return currentViewController;
            } else {

                return currentViewController;
            }
        }

    }
    return currentViewController;
}
@end
